package com.zhihaoliang.map.base

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Tab
import androidx.compose.material.TabRow
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.zhihaoliang.map.R
import com.zhihaoliang.map.home.CraneScreen
import com.zhihaoliang.map.ui.theme.CraneTheme
import java.util.*

/**
 * @author 支豪亮
 * @date 2023/1/12 15:26
 */


/**
 *  左上角的Logo 和 Tab
 *  @param onMenuClicked 点击Logo 展示抽屉
 *  @param children 用于展示 tab
 */
@Composable
fun CraneTabBar( onMenuClicked: () -> Unit, children: @Composable (Modifier) -> Unit) {
    Row { // Separate Row as the children shouldn't have the padding
        Row(Modifier.padding(top = 8.dp)) {

            Image(painter = painterResource(id = R.drawable.ic_menu), contentDescription = stringResource(id = R.string.cd_menu), modifier = Modifier
                    .padding(top = 8.dp)
                    .clickable(onClick = onMenuClicked))

            Spacer(modifier = Modifier.width(8.dp))

            Image(
                painter = painterResource(id = R.drawable.ic_crane_logo),
                contentDescription = null
            )

            children(Modifier
                             .weight(1f)
                             .align(Alignment.CenterVertically)
            )
        }
    }

}

/**
 * TabRow 的渲染
 * @param modifier 页面的装饰
 * @param titles TabRow item 展示的文字
 * @param tabSelected 当前选中的 TabRow
 * @param onTabSelected 点击的操作
 */
@Composable
fun CraneTabs(modifier: Modifier = Modifier, titles: List<String>, tabSelected: CraneScreen, onTabSelected: (CraneScreen) -> Unit = {}) {
    TabRow(selectedTabIndex = tabSelected.ordinal, modifier = modifier, contentColor = MaterialTheme.colors.onSurface, indicator = { }, divider = { }) {
        titles.forEachIndexed { index, title ->
            val selected = index == tabSelected.ordinal

            var textModifier = Modifier.padding(vertical = 8.dp, horizontal = 16.dp)
            if (selected) {
                textModifier = Modifier
                        .border(BorderStroke(2.dp, Color.White), RoundedCornerShape(16.dp))
                        .then(textModifier)
            }

            Tab(selected = selected, onClick = { onTabSelected(CraneScreen.values()[index]) }) {
                Text(modifier = textModifier, text = title.uppercase(Locale.getDefault()))
            }
        }
    }
}


@Composable
@Preview
private fun CraneTabBarPreview(){
    CraneTabBar(onMenuClicked = { }) {
    }
}

@Composable
@Preview
private fun CraneTabsPreview() {
    CraneTheme {
        CraneTabs(titles = CraneScreen.values().map { it.name }, tabSelected = CraneScreen.Fly)
    }
}